package com.fivefu.core.camera.services.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.camera.entity.DbSceneEvent;
import com.fivefu.core.camera.mapper.DbSceneEventMapper;
import com.fivefu.core.camera.services.DbSceneEventService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 场景工单表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-19 19:38:06
 */
@Service
public class DbSceneEventServiceImpl extends ServiceImpl<DbSceneEventMapper, DbSceneEvent> implements DbSceneEventService {

    /**
     * 通过场景标识查询场景工单配置
     * @param sceneCode 场景标识
     * @param type 生成工单类型
     * @return
     */
    @Override
    public List<DbSceneEvent> querySceneEventBySceneCode(String sceneCode,String type){
        QueryWrapper<DbSceneEvent> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (StrUtils.isNotNull(sceneCode)) {
            wrapper.eq("code", sceneCode);
        }
        if (StrUtils.isNotNull(type)){
            wrapper.eq("type",type);
        }
        List<DbSceneEvent> sceneList = baseMapper.selectList(wrapper);
        return sceneList;
    }

    /**
     * 编辑场景工单配置信息
     * @param sceneEvent 场景工单配置信息
     * @param userid 操作用户编号
     */
    @Override
    public Long editSceneEvent(DbSceneEvent sceneEvent, String userid){
        LocalDateTime time = LocalDateTime.now();

        sceneEvent.setUpdatedBy(userid);
        sceneEvent.setUpdatedTime(time);
        if (sceneEvent.getId() != null){
            baseMapper.updateById(sceneEvent);
        }else{
            sceneEvent.setCreatedBy(userid);
            sceneEvent.setCreatedTime(time);
            sceneEvent.setIsDelete(0);
            baseMapper.insert(sceneEvent);
        }
        return sceneEvent.getId();
    }

    /**
     * 删除多余的生成工单配置
     * @param sceneCode 场景标识
     * @param saveSceneEventIds 保留工单配置编号集合
     * @param userid 操作用户编号
     */
    @Override
    public void removeSurplusSceneEvent(String sceneCode, List<Long> saveSceneEventIds, String userid){
        QueryWrapper<DbSceneEvent> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (StrUtils.isNotNull(sceneCode)) {
            wrapper.eq("code", sceneCode);
        }
        if (saveSceneEventIds != null && saveSceneEventIds.size() > 0){
            wrapper.notIn("id",saveSceneEventIds);
        }

        DbSceneEvent event = new DbSceneEvent();
        event.setIsDelete(1);
        event.setUpdatedBy(userid);
        event.setUpdatedTime(LocalDateTime.now());
        baseMapper.update(event, wrapper);
    }

    /**
     * 格式化工单配置信息
     * @param event 场景工单配置信息
     * @return
     */
    @Override
    public Map<String, Object> formatSceneEvent(DbSceneEvent event){
        Map<String,Object> map = JSON.parseObject(JSON.toJSONBytes(event, SerializerFeature.WriteMapNullValue), Map.class);
        if (StrUtils.isNotNull(event.getEventType())){
            map.put("eventTypeName","");
        }
        if (StrUtils.isNotNull(event.getEventBigtype())){
            map.put("eventBigTypeName","");
        }
        if (StrUtils.isNotNull(event.getEventSmalltype())){
            map.put("eventSmallTypeName","");
        }
        return map;
    }

}
